package com.example.mobileeducationstudentv;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Spinner;
import android.app.Activity;

public class Help extends Activity {

	// variable declaration
	private static final String TAG = "HelpActivity";
	Button contactSend;
	Button backButton;
	EditText contactSubject;
	EditText contactMessage;
	private Spinner subjectSpinner;
	File[] list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// initializing layout
		setContentView(R.layout.help);
		addListenerOnSpinnerItemSelection();
		// initialize buttons
		backButton = (Button) findViewById(R.id.btnBack);
		contactSend = (Button) findViewById(R.id.contactSend);
		subjectSpinner = (Spinner) findViewById(R.id.subjectSpinner);
		contactMessage = (EditText) findViewById(R.id.contactMessage);
		// try to add items on spinner
		try {
			addItemsOnSpinner();
		}
		// display error message if items could not be fetched
		catch (Exception e) {
			Toast.makeText(getApplicationContext(),
					"Error: Subjects could not be found", Toast.LENGTH_LONG)
					.show();
		}
		contactSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// variable setting
				// port number of destination emulator (can be changed to a
				// phone #)
				String phoneNo = "5556";
				// get selected value from dropdown menu
				String subject = subjectSpinner.getSelectedItem().toString();
				// get message content
				String message = contactMessage.getText().toString();
				// concat subject with message
				String sms = subject + ";" + message;

				// if the subject isn't blank and the message isn't blank
				if (subject.length() > 0 && message.length() > 0) {
					// if the message is shorter than 160 characters
					if (sms.length() < 160) {
						// try to send the SMS with the message contents
						// starting with the subject title followed by a - and
						// then the message.
						try {
							SmsManager smsManager = SmsManager.getDefault();
							smsManager.sendTextMessage(phoneNo, null, sms,
									null, null);
							Toast.makeText(getApplicationContext(), "SMS Sent",
									Toast.LENGTH_LONG).show();
							finish();
							// Intent i = new
							// Intent(Help.this,MainActivity.class);
							// startActivity(i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
							Log.i(TAG, "Message sent sucessfully!");
						} catch (Exception e) {
							Toast.makeText(getApplicationContext(),
									"SMS Sending Failed", Toast.LENGTH_LONG)
									.show();
							Log.e(TAG, "Critical error, message sending failed");
							e.printStackTrace();
						}
						// if message is longer than 160 characters, display pop
						// up to enter a shorter message.
					} else {
						Toast.makeText(getApplicationContext(),
								"Please enter a shorter message",
								Toast.LENGTH_LONG).show();
						Log.i(TAG, "Message too long to be sent");
					}
					// if message/subject is blank, display pop up to fill in
					// all the fields.
				} else {
					Toast.makeText(getApplicationContext(),
							"Please fill in all the fields", Toast.LENGTH_LONG)
							.show();
					Log.i(TAG,
							"One or more fields (subject/message) left blank");
				}

			}
		});

		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void addListenerOnSpinnerItemSelection() {
		subjectSpinner = (Spinner) findViewById(R.id.subjectSpinner);
		subjectSpinner
				.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}

	// add folders from main subject folder onto spinner
	public void addItemsOnSpinner() {
		final String sdcard = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/MobileEducation/Subjects/";
		list = GetFiles(sdcard);
		List<String> myFiles = new ArrayList<String>();
		if (list.length > 0) {
			for (int i = 0; i < list.length; i++) {
				myFiles.add(list[i].getName());
			}
		}
		myFiles.add("Other");
		ArrayAdapter<String> fileAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, myFiles);
		fileAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		subjectSpinner.setAdapter(fileAdapter);
	}

	public File[] GetFiles(String DirectoryPath) {

		File f = new File(DirectoryPath);
		f.mkdirs();
		File[] files = f.listFiles();
		return files;
	}

}
